<template>
  <div id="questionSubmitView">
    <el-card class="card">
      <el-form class="search-form" :data="searchParams" :inline="true">
        <el-form-item prop="questionId" label="题号" style="min-width: 240px">
          <el-input v-model="searchParams.problemId" placeholder="请输入" />
        </el-form-item>

        <el-form-item
          field="language"
          label="编程语言"
          style="min-width: 240px"
        >
          <el-select
            v-model="searchParams.language"
            :style="{ width: '320px' }"
            placeholder="选择编程语言"
          >
            <el-option label="请选择" value=""></el-option>
            <el-option label="java" value="java"></el-option>
            <el-option label="cpp" value="cpp"></el-option>
            <el-option label="go" value="go"></el-option>
            <el-option label="html" value="html"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-divider size="0" />
      <el-table :ref="tableRef" :data="dataList">
        <!--      <template #judgeInfo="{ record }">-->
        <!--        {{ JSON.stringify(record.judgeInfo) }}-->
        <!--      </template>-->
        <!--      <template #createTime="{ record }">-->
        <!--        {{ moment(record.createTime).format('YYYY-MM-DD') }}-->
        <!--      </template>-->

        <el-table-column
          prop="problemId"
          label="题号"
          width="100"
          align="center"
        />
        <el-table-column
          prop="language"
          label="编程语言"
          width="100"
          align="center"
        />
        <el-table-column
          prop="status"
          label="题目状态"
          width="100"
          align="center"
        />

        <el-table-column label="判题状态" align="center">
          <template #default="scope">
            <el-tag type="success" effect="dark" v-if="scope.row.status === 3"
              >通过
            </el-tag>
            <el-tag
              type="danger"
              effect="dark"
              v-else-if="scope.row.status === 2"
              >失败
            </el-tag>
            <el-tag
              type="warning"
              effect="dark"
              v-else-if="scope.row.status === 1"
              >判题中
            </el-tag>
            <el-tag type="info" effect="dark" v-else>待判题</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="判题信息" align="center">
          <template #default="scope">
            {{ scope.row.judgeInfo.status }}
          </template>
        </el-table-column>

        <el-table-column label="内存" align="center">
          <template #default="scope">
            {{ scope.row.judgeInfo.time }}
          </template>
        </el-table-column>
        <el-table-column label="内存" align="center">
          <template #default="scope">
            {{ scope.row.judgeInfo.memory }}
          </template>
        </el-table-column>

        <el-table-column label="通过率" align="center">
          <template #default="scope">
            {{
              `${
                scope.row.judgeInfo.total
                  ? formatNumber(
                      Number(scope.row.judgeInfo.pass) /
                        Number(scope.row.judgeInfo.total),
                      2,
                    )
                  : '0'
              }% (${scope.row.judgeInfo.pass}/${scope.row.judgeInfo.total})`
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="userId"
          label="用户id"
          width="100"
          align="center"
        />
        <el-table-column label="创建时间" align="center">
          <template #default="scope">
            {{ moment(scope.row.createTime).format('YYYY-MM-DD') }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pager-wrapper">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          :page-size="searchParams.pageSize"
          :currentPage="searchParams.pageNum"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from 'vue'
import { useRouter } from 'vue-router'
import moment from 'moment'
import {
  ProblemSubmitControllerService,
  ProblemSubmitQueryRequest,
  ProblemSubmitVo,
} from '../../../generated'
import { ElMessage } from 'element-plus'

const tableRef = ref()
const formatNumber = (src: number, pos: number) => {
  const data = Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos)
  return data.toFixed(2)
}
const dataList = ref<ProblemSubmitVo[]>([])
const total = ref(0)

const handleCurrentChange = (val: number) => {
  console.log('修改了page', val)
  searchParams.value.pageNum = val
  loadData()
}
const searchParams = ref<ProblemSubmitQueryRequest>({
  problemId: undefined,
  uid: undefined,
  pageSize: 10,
  pageNum: 1,
  sortField: 'create_time',
  sortOrder: 'descend',
})

const loadData = async () => {
  const res = await ProblemSubmitControllerService.listProblemSubmitVoByAdmin(
    searchParams.value,
  )
  console.log(res)
  if (res.code === 200) {
    dataList.value = res.data?.records as ProblemSubmitVo[]
    total.value = res.data?.total as number
  } else {
    ElMessage({
      type: 'error',
      message: res.message,
    })
  }
}

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
// watchEffect(() => {
//   loadData()
// })

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData()
})

const router = useRouter()

/**
 * 跳转到做题页面
 * @param question
 */
// const toQuestionPage = (question: ) => {
//   router.push({
//     path: `/view/question/${question.id}`,
//   })
// }

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    pageNum: 1,
  }

  loadData()
}
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}

.pager-wrapper {
  margin-top: 20px;
  float: right;
}

.search-form {
  margin-top: 20px;
}

.card {
  padding: 10px;
}
</style>
